package org.example.Controller;

import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ds")
public class DeepSeekChatController {
    @Autowired
    private OpenAiChatModel chatModel;
    @GetMapping("/chat")
    public String generate(String message){
        return chatModel.call( message);
    }
    @GetMapping("/chatByPrompt")
    public String chatByPrompt(String message){
        Prompt prompt=new Prompt(message);
        ChatResponse response=chatModel.call( prompt);
        return response.getResult().getOutput().getText();
    }
    @RequestMapping("/role")
    public String role(String message){
        SystemMessage systemMessage=new SystemMessage("你是一个AI助手");
        UserMessage userMessage = new UserMessage(message);
        Prompt prompt=new Prompt(systemMessage,userMessage);
        ChatResponse response = chatModel.call(prompt);
        return response.getResult().getOutput().getText();
    }
    @RequestMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String message){
        Prompt prompt=new Prompt(message);
        Flux<ChatResponse> response=chatModel.stream(prompt);
        return response.map(x->x.getResult().getOutput().getText());
    }
}
